﻿Imports System.Data.SqlClient
Public Class frmCatalogoBombasDetalle
    Dim Bombas As New clsCatalogoBombas
    Dim Bitacora As New clsActividad
    Dim Usuario As New clsUsuarioPrincipal
    Private mstrCondicion As String
    Public WriteOnly Property Condicion() As String
        Set(ByVal value As String)
            mstrCondicion = value
        End Set
    End Property

    Private Sub frmCatalogoBombasDetalle_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Bitacora.RegistraActividad("Cerró al catálogo detallado de Bombas")
        Usuario.Ubicacion("Catálogo de Bombas")
    End Sub
    Private Sub frmCatalogoBombasDetalle_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        ToolStrip1.Cursor = Cursors.Hand
        ToolStrip1.RenderMode = System.Windows.Forms.ToolStripRenderMode.System

        If BombaNueva = True Then
            txtBomba.Text = ""
        Else
            txtBomba.Text = BombaSelect
        End If


        'Actualizar()
        CargaBombas()
        CargaAlmacenes()

        Bitacora.RegistraActividad("Ingresó al catálogo detallado de Bombas")
        Usuario.Ubicacion("Catálogo detallado de Bombas")
    End Sub
    Sub Actualizar()
        Dim strNombre As String = ""
        If txtBomba.Text <> "" Then
            strNombre = Me.txtBomba.Text
            Bombas.Nombre = strNombre
        Else
            strNombre = ""
            Bombas.Nombre = strNombre
        End If
        Bombas.Buscar()
        Scatter()
    End Sub
    Private Sub Scatter()
        txtUbicacion.Text = Bombas.Ubicacion
        txtNumeracion.Text = Bombas.Numeracion_Actual
        txtAlmacen.Text = BuscarStrDatoCitra("Almacenes", "cNombreA01", "IdAlmacen=" & Bombas.IdAlmacen)
    End Sub
    Sub CargaAlmacenes()
        Windows.Forms.Cursor.Current = Cursors.WaitCursor
        dgBuscarRuta.Rows.Clear()

        Dim Filtro As String
        Filtro = UCase(txtBuscarRuta.Text)
        Filtro = "%" & Filtro.Replace(" ", "%") & "%"

        Dim cnConn As New SqlConnection
        Dim strSql As String
        Dim cmdBuscar As New SqlCommand
        Dim rdBuscar As SqlDataReader

        Try
            cnConn.ConnectionString = CitraConnection
            strSql = "Select * from Almacenes WHERE UPPER(cNombreA01) LIKE '" & Filtro & "' " & mstrCondicion

            cmdBuscar.Connection = cnConn
            cmdBuscar.CommandText = strSql


            cnConn.Open()
            rdBuscar = cmdBuscar.ExecuteReader

            Do While rdBuscar.Read()
                dgBuscarRuta.Rows.Add(rdBuscar("CNombreA01").ToString.Trim)
                dgBuscarRuta.CurrentCell = dgBuscarRuta.Rows(0).Cells(0)
            Loop
        Catch ex As Exception
            MessageBox.Show(ex.Message, "System Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End Try
    End Sub
    Sub CargaBombas()
        Windows.Forms.Cursor.Current = Cursors.WaitCursor
        dgBuscarBombas.Rows.Clear()

        Dim Filtro As String
        Filtro = UCase(txtBuscarBombas.Text)
        Filtro = "%" & Filtro.Replace(" ", "%") & "%"

        Dim cnConn As New SqlConnection
        Dim strSql As String
        Dim cmdBuscar As New SqlCommand
        Dim rdBuscar As SqlDataReader

        Try
            cnConn.ConnectionString = CitraConnection
            strSql = "Select * from Bombas WHERE UPPER(Nombre) LIKE '" & Filtro & "' " & mstrCondicion

            cmdBuscar.Connection = cnConn
            cmdBuscar.CommandText = strSql


            cnConn.Open()
            rdBuscar = cmdBuscar.ExecuteReader

            Do While rdBuscar.Read()
                dgBuscarBombas.Rows.Add(rdBuscar("Nombre").ToString.Trim)
                ' dgBuscarBombas.CurrentCell = dgBuscarBombas.Rows(0).Cells(1)
            Loop
        Catch ex As Exception
            MessageBox.Show(ex.Message, "System Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End Try
    End Sub
    Private Sub txtBuscarBombas_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtBuscarBombas.KeyPress
        If e.KeyChar = Chr(Keys.Enter) Then
            SeleccionarBomba()
        ElseIf e.KeyChar = Chr(Keys.Escape) Then
            panelBombas.Visible = False
            txtBomba.SelectAll()
            txtBomba.Focus()

        End If

    End Sub

    Private Sub txtBuscarBombas_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtBuscarBombas.TextChanged
        CargaBombas()
    End Sub

    Private Sub txtBuscarRuta_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtBuscarRuta.KeyPress
        If e.KeyChar = Chr(Keys.Enter) Then
            SeleccionarAlmacen()
        ElseIf e.KeyChar = Chr(Keys.Escape) Then
            PanelRutas.Visible = False
            txtAlmacen.SelectAll()
            txtAlmacen.Focus()
        End If
    End Sub
    Private Sub txtBuscarRuta_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtBuscarRuta.TextChanged
        CargaAlmacenes()

    End Sub
    Private Sub dgBuscarRuta_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgBuscarRuta.DoubleClick
        seleccionaralmacen()
    End Sub

    Private Sub dgBuscarRuta_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles dgBuscarRuta.KeyPress
        seleccionaralmacen()
    End Sub
    Private Sub dgBuscarBombas_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgBuscarBombas.DoubleClick
        seleccionarbomba()
    End Sub

    Private Sub dgBuscarBombas_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles dgBuscarBombas.KeyPress
        seleccionarbomba()
    End Sub
    Sub SeleccionarAlmacen()
        Dim valorCell As Integer
        Dim valorRow As DataGridViewRow

        valorCell = dgBuscarRuta.CurrentCell.ColumnIndex
        valorRow = dgBuscarRuta.CurrentRow

        Dim AlmacenMostrar As String = dgBuscarRuta.Rows(valorRow.Index).Cells(0).Value.ToString.Trim
        txtAlmacen.Text = AlmacenMostrar

        PanelRutas.Visible = False
        txtAlmacen.SelectAll()
        txtAlmacen.Focus()
    End Sub
    Sub seleccionarbomba()
        Dim valorCell As Integer
        Dim valorRow As DataGridViewRow

        valorCell = dgBuscarBombas.CurrentCell.ColumnIndex
        valorRow = dgBuscarBombas.CurrentRow

        Dim BombaMostrar As String = dgBuscarBombas.Rows(valorRow.Index).Cells(0).Value.ToString.Trim
        txtBomba.Text = BombaMostrar

        panelBombas.Visible = False
        txtBomba.SelectAll()
        txtBomba.Focus()
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        panelBombas.Visible = True
        txtBuscarBombas.Clear()
        txtBuscarBombas.Focus()

    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        PanelRutas.Visible = True
        txtBuscarRuta.Clear()
        txtBuscarRuta.Focus()
    End Sub
    Private Sub Gatter()
        Dim IdBomba As Integer = BuscarIntDatoCitra("Bombas", "IdBomba", "Nombre='" & txtBomba.Text.Trim & "'")
        If IdBomba > 0 Then
            Bombas.IdBomba = IdBomba
        Else
            Bombas.IdBomba = (BuscarIntDatoCitra("Select Max(IdBomba) as IdBomba FROM Bombas")) + 1
        End If
        Bombas.Nombre = txtBomba.Text.Trim
        Bombas.Ubicacion = txtUbicacion.Text.Trim
        Bombas.Numeracion_Actual = txtNumeracion.Text
        Bombas.IdAlmacen = BuscarIntDatoCitra("Almacenes", "IdAlmacen", "cNombreA01='" & txtAlmacen.Text.Trim & "'")
    End Sub

    Private Sub ToolStripButton1_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButton1.Click
        Close()

    End Sub
    Sub Guardar()
        Gatter()
        Bombas.Guardar()
        frmCatalogoBombas.CargaBombas()
    End Sub
   
    Private Sub ToolStripButton2_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButton2.Click
        Guardar()

    End Sub

    Private Sub RegresarToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles RegresarToolStripMenuItem.Click
        Close()

    End Sub

    Private Sub NuevaToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles NuevaToolStripMenuItem.Click
        txtBomba.Text = ""
        txtBomba.Focus()

    End Sub

    Private Sub GuardarToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles GuardarToolStripMenuItem.Click
        Guardar()

    End Sub

    Private Sub txtBomba_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtBomba.TextChanged
        Actualizar()

    End Sub

    Private Sub MenuStrip1_ItemClicked(sender As System.Object, e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles MenuStrip1.ItemClicked

    End Sub
End Class